//
//  RegiseterTwoViewController.m
//  1243
//
//  Created by ym on 16/4/21.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "RegiseterTwoViewController.h"
#import "NetWorkTools.h"
#import "AddressPickView.h"

@interface RegiseterTwoViewController ()<UITextFieldDelegate>
{
    NSMutableArray *addressArr;
    NSString *userName;
}
@property (weak, nonatomic) IBOutlet UITextField *shouHuoRenPhoneNumTextField;

@property (weak, nonatomic) IBOutlet UITextField *shouHuoRenAddressTextField;
@property (weak, nonatomic) IBOutlet UITextField *tuiJianMaNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *shouHuoRenNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *kaiHuBankTextField;
@property (weak, nonatomic) IBOutlet UITextField *inPutBankNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *kaiHuHangBankTextField;

@property (weak, nonatomic) IBOutlet UILabel *TuiJian;


@end

@implementation RegiseterTwoViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
    
    self.shouHuoRenPhoneNumTextField.delegate = self;
    self.shouHuoRenPhoneNumTextField.tag = 101;
    
    self.shouHuoRenAddressTextField.delegate = self;
    self.shouHuoRenAddressTextField.tag = 100;
    
    self.tuiJianMaNumberTextField.delegate = self;
    self.tuiJianMaNumberTextField.tag = 101;
     NSDictionary *dict = [ReadLoginData readLoginData];
    if ([self.type isEqualToString:@"推荐会员"]) {
 
        self.tuiJianMaNumberTextField.hidden = YES;
        self.TuiJian.hidden = YES;
        
        if (self.str.length == 0) {
            self.tuiJianMaNumberTextField.text =dict[@"UserName"];
        }else{
            self.tuiJianMaNumberTextField.text = self.str;
            
        }
        
        
    }
}

- (IBAction)AddressChose:(id)sender {
    AddressPickView *addressPickView = [AddressPickView shareInstance];
    [self.view addSubview:addressPickView];
    addressPickView.block = ^(NSString *province,NSString *city,NSString *town){
        //        NSLog(@"%@%@%@",province,city,town);
        [sender setTitle:[NSString stringWithFormat:@"%@%@%@",province,city,town] forState:UIControlStateNormal];
        addressArr = [NSMutableArray arrayWithObjects:province,city,town, nil];
        
        
    };
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self resignFirstResponderTextField];
}

- (void)resignFirstResponderTextField {
    [self.shouHuoRenAddressTextField resignFirstResponder];
    [self.shouHuoRenPhoneNumTextField resignFirstResponder];
    [self.tuiJianMaNumberTextField resignFirstResponder];
    [self.shouHuoRenNameTextField resignFirstResponder];
    [self.kaiHuBankTextField resignFirstResponder];
    [self.inPutBankNumberTextField resignFirstResponder];
    [self.kaiHuHangBankTextField resignFirstResponder];
}

- (IBAction)ZhuCe:(id)sender {
    
    
    
    if (self.shouHuoRenAddressTextField.text.length == 0 || self.shouHuoRenPhoneNumTextField.text.length ==0 || self.tuiJianMaNumberTextField.text.length == 0 || self.shouHuoRenNameTextField.text.length == 0 || self.kaiHuBankTextField.text.length == 0 || self.inPutBankNumberTextField.text.length == 0 ||self.kaiHuHangBankTextField == 0 || addressArr.count == 0 ) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完整信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        
        [alert show];
        
    }else{
        [self netWork];
    }

    
}

- (void)netWork{
    
    if ([self.type isEqualToString:@"注册"]) {
         NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.dic];
        [dic setValue:self.tuiJianMaNumberTextField.text forKey:@"V_Name"];
        [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dic AndMenthodName:@"ADDUSER" completeBlock:^(id object) {
            NSLog(@"%@",object);
            
            
            NSString *str = [NSString stringWithFormat:@"%@",object[@"BackData"]];
            
            userName = str;
            if ([object[@"Id"] integerValue] == 0) {
            
                [self data];
            }else{
                NSString *string = [object objectForKey:@"Message"];
                
                string = [string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                
                
                [alert show];
                
            }
            
        }];
    }else{
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.dic];        
        [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dic AndMenthodName:@"ADDUSER" completeBlock:^(id object) {
              NSLog(@"%@",object);
            NSString *str = [NSString stringWithFormat:@"%@",object[@"BackData"]];
 
            userName = str;
            if ([object[@"Id"] integerValue] == 0) {
                // 用户注册成功
                
                [self data];
            }else{
                
                NSString *string = [object objectForKey:@"Message"];
                
                string = [string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                alert.tag = 1111;
                
                      [alert show];
            }
            
        }];
    }
    
    
}
- (void)data{
    
//    NSDictionary *dict = [ReadLoginData readLoginData];
//    NSString *userNumber = [dict objectForKey:@"Number"];
    
    /*
     [self.shouHuoRenAddressTextField resignFirstResponder];
     [self.shouHuoRenPhoneNumTextField resignFirstResponder];
     [self.tuiJianMaNumberTextField resignFirstResponder];
     [self.shouHuoRenNameTextField resignFirstResponder];
     [self.kaiHuBankTextField resignFirstResponder];
     [self.inPutBankNumberTextField resignFirstResponder];
     [self.kaiHuHangBankTextField resignFirstResponder];

     */
    
    NSDictionary *params = @{
                             @"U_Number":userName,
                             @"Bank_Info":self.inPutBankNumberTextField.text,
                             @"Bank_Code":self.inPutBankNumberTextField.text,
                             @"WW":self.kaiHuBankTextField.text,  // 开户行
                             @"Province":addressArr[0],
                             @"City":addressArr[1],
                             @"Countory":addressArr[2],
                             @"ID_Card":self.idcard,
                             @"Address":self.shouHuoRenAddressTextField.text,
                             @"ContactName":self.shouHuoRenNameTextField.text,
                             @"ContactPhone":self.shouHuoRenPhoneNumTextField.text,
                             @"IsDefault":@"1"
                             };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"PERFECTIONUSERINFO" completeBlock:^(id object) {
               NSLog(@"object%@",object);
        
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"Message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
     //   [alview show];
        
        if ([[NSString stringWithFormat:@"%@",object[@"Id"]] isEqualToString:@"0"]) {
            UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"注册成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alview show];
        }else{
            UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"Message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alview show];
            
        }
        
    }];
    
    
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag == 101|| textField.tag == 100) {
        CGRect frame = textField.frame;
        int offset = frame.origin.y + 80 - (self.view.frame.size.height - 216.0);//键盘高度216
        
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        if(offset > 0)
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        
        [UIView commitAnimations];
        
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    
    self.view.frame =CGRectMake(0, 64, width, height);
    //    NSLog(@"%@", textField.text);
}




@end
